'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en', 
        'datetimepicker', 'datetimepickerCN'], function ($) {
  return {
    
    init: function () {
    	this.initSelect();
      this.initPage();
      this.initSearch();
      this.initChange();
    },

    initSelect:function(){
    	 $('select[name="type"]').select2({
         placeholder : '请选择类型',
         minimumResultsForSearch : -1,
         allowClear : true
       });
       
       $('select[name="level"]').select2({
         placeholder : '请选择供方级别',
         minimumResultsForSearch : -1,
         allowClear : true
       });
    },
    
    initPage: function () {
      
    },

    initSearch: function () {
    	var that=this;
    	$('select[name="level"]').on("change", function(e){
    		that._getAjaxData();
      });
      
    	$('select[name="type"]').on("change", function(e){
  	 	  that._getAjaxData();
      });
    },
    
    _getAjaxData: function() {
    	var type = $('select[name="type"]').val().trim();
   	  var level = $('select[name="level"]').val().trim();
   	  window.location.href = window.__CTX__.root + "/supplier-profile?filter=true&type="+type+"&level="+level
    },
    
    initChange: function () {
      $(document).on("shown.bs.modal", "#change", function (e) {

        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
        
        var $form = $(e.currentTarget).find("form")              //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            
            $form.trigger("validate.ajax.submit");
            var validate = $.bt_validate.result[$.bt_validate.form_id];
            return validate;
            
          }
        })
        
      });
    }
    
  }
})